package com.showcai.controller.admin;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.showcai.module.Book;
import org.springframework.stereotype.Controller;

public class ExclePoiBook {
    /** 
          * 针对Book类进行导出的操作 
          *  
          * @param list 
          */
    public void excleOut(List<Book> list){
        // 创建Excel文档  
        HSSFWorkbook hwb = new HSSFWorkbook();
        // 通过excle对象创建一个选项卡对象  
        HSSFSheet sheet=hwb.createSheet("sheet1");
        Book book =null;
        // 循环list创建行  
        for(int i=0;i<list.size();i++){
            // 新建一行  
            HSSFRow row=sheet.createRow(i);
            book =list.get(i);
            // 设置i+1行第0列的数据  
            row.createCell(0).setCellValue(book.getId());
            // 设置i+1行第1列的数据  
            row.createCell(1).setCellValue(book.getName());
            // 设置i+1行第2列的数据  
            row.createCell(2).setCellValue(book.getAuthor());
        }
        OutputStream out=null;
        try{
            out=new FileOutputStream("e:/excleTest/bookPoi.xls");
            hwb.write(out);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                out.close();
            }catch(IOException e){
                // TODO Auto-generated catch block  
                e.printStackTrace();
            }
        }
    }

    /** 
          * 针对Book类进行导入的操作 
          *  
          * @return 
          */
    public List<Book> excleIn(File file){
        List<Book> list=new ArrayList<>();
        Book book=null;
        try{
            InputStream is = new FileInputStream(file);
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
            // 获取选项卡对象 第0个选项卡 , 因为我们这里只有一个选项卡，如果你每个选项卡的内容是一样，可以通过循环取出  
            HSSFSheet   hssfSheet=hssfWorkbook.getSheetAt(0);
            // 循环取出每行的值
            for(int rowNum=0;rowNum<=hssfSheet.getLastRowNum();rowNum++){
                HSSFRow hssfRow=hssfSheet.getRow(rowNum);
                book=new Book();
                //注意Poi读取的内容是有类型的，处理起来也jxl有所不同  
                book.setId((int)hssfRow.getCell(0).getNumericCellValue());
                book.setName(hssfRow.getCell(1).getStringCellValue());
                book.setAuthor(hssfRow.getCell(2).getStringCellValue());
                list.add(book);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
        return list;
    }
    public static void main(String[] args){
        ExclePoiBook exclePoiBook =new ExclePoiBook();
        List<Book> list = new ArrayList<>();
        Book book2=new Book();
        book2.setId(1);
        book2.setName("书本名1");
        book2.setAuthor("张三");
        Book book3=new Book();
        book3.setId(2);
        book3.setName("书本名2");
        book3.setAuthor("李四");
        list.add(book2);
        list.add(book3);
        exclePoiBook.excleOut(list);
//        List<Book>books=exclePoiBook.excleIn();
//        for(int i=0;i<books.size();i++){
//            System.out.println(books.get(i).getId()+""+books.get(i).getName()+""+books.get(i).getAuthor());
//        }
    }
}